using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System.Data;

namespace AdverseEventReportSystem.Repository 
{
    public class AdverseEventReasonRepository : BaseRepository<AdverseEventReason>
    {

        #region 构造函数

        private static AdverseEventReasonRepository _instance;
        public static AdverseEventReasonRepository GetInstance()
        {
            if (_instance == null)
            {
                _instance = new AdverseEventReasonRepository();
            }
            return _instance;
        }

        #endregion

        #region Add

        public int Add(AdverseEventReason entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int Id = context.Insert<AdverseEventReason>("AdverseEventReason", entity)
                    .AutoMap(x => x.ID)
                    .ExecuteReturnLastId<int>();
            return Id;
        }

        #endregion

        #region Update

        public int Update(AdverseEventReason entity, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            int rowsAffected = context.Update<AdverseEventReason>("AdverseEventReason", entity)
                    .AutoMap(x => x.ID)
                    .Where(x => x.ID)
                    .Execute();
            return rowsAffected;
        }

        #endregion

        #region 获取单个实体 通过主键ID

        /// <summary>
        /// 获取单个实体 通过主键ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual AdverseEventReason GetQuerySingleByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "SELECT * FROM AdverseEventReason WHERE ID=@0";
            AdverseEventReason obj = GetQuerySingle(sqlStr, context, objects);
            return obj;
        }

        #endregion

        #region 删除操作  通过ID

        /// <summary>
        /// 删除操作  通过ID
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByID(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "DELETE FROM AdverseEventReason WHERE ID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 根据事件类型删除

        /// <summary>
        /// 根据事件类型删除
        /// </summary>
        /// <param name="typeID">事件类型ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual int DelByTypeID(int typeID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = typeID;
            string sqlStr = "DELETE FROM AdverseEventReason WHERE TypeID=@0";
            return Del(sqlStr, context, objects);
        }

        #endregion

        #region 获取实体DataTable

        /// <summary>
        /// 根据事件类型ID
        /// </summary>
        /// <param name="typeID">事件类型ID</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual DataTable GetAdverseEventReasonTable(int typeID, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = typeID;
            string sqlStr = @"with tb as
                             (
                               select top 100 ID,ParentId,Reason as Reason1,cast(Reason as varchar(max)) Reason,1 Level,Sort,cast(ID as varchar(max)) Path, cast(right('00000'+ltrim(row_number() over (order by Sort,Id)),5) as varchar(max))PathSort from AdverseEventReason where ParentId = 0 and TypeID = @0 order by Sort,Id
                               union all
                               select a.ID,a.ParentId,a.Reason as Reason1,b.Reason+'#'+cast(a.Reason as varchar(max)) Reason,Level+1,a.Sort,b.Path +'#'+ cast(a.ID as varchar(max)) Path,PathSort + cast(right('00000'+ltrim(a.Sort),5) as varchar(max)) from AdverseEventReason a join tb b on a.parentid=b.Id
                             )
                             select a.ID AS ReasonID,a.ParentId,b.Reason1,b.Reason,b.Level,b.Path,b.PathSort,0 IsChecked,(select count(1) from AdverseEventReason where ParentId = a.Id) Cnt from AdverseEventReason a,tb b 
                             where a.ID=b.ID
                             order by PathSort";

            return GetDataTable(sqlStr, context, objects);
        }

        #endregion

        #region 获取实体List

        /// <summary>
        /// 根据ID集合
        /// </summary>
        /// <param name="idList">ID集合</param>
        /// <param name="context">数据库连接对象</param>
        /// <returns></returns>
        public virtual List<AdverseEventReason> GetAdverseEventReasonList(List<int> idList, IDbContext context = null)
        {
            Object[] objects = new Object[1];
            objects[0] = string.Join(",", idList.ToArray());
            string sqlStr = "EXEC('SELECT * FROM AdverseEventReason WHERE ID IN('+@0+')')";
            return GetQueryMany(sqlStr, context, objects);
        }

        #endregion

        #region 获取子级的数量

        /// <summary>
        /// 获取子级的数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual int GetChildrenCount(int id, IDbContext context = null)
        {
            if (context == null) context = Db.GetInstance().Context();
            Object[] objects = new Object[1];
            objects[0] = id;
            string sqlStr = "SELECT Count(1) FROM AdverseEventReason WHERE ParentID=@0";
            int count = GetCount(sqlStr, context, objects);
            return count;
        }

        #endregion
    }
}





